import WxValidate from "./../../lib/WxValidate";
Page({
  data: {
    formInfo: {
      name: "",
      phone: "",
      address: "",
      code: "",
    },
    code: "",
    searchForm: {
      name: "",
      tel: "",
    },
  },

  toSearch(e) {
    const { value } = e.detail;
    console.log("绑定回车事件", value);
  },

  subimt() {},

  input() {},

  toSelectCountry() {
    wx.hideKeyboard();
    wx.navigateTo({
      url: "/page/country/index",
    });
  },

  // 方法一：监听 input 事件更新数据
  bindInputEvent(event) {
    const { value } = event.detail;
    const { key } = event.currentTarget.dataset;
    const { formInfo } = this.data;
    const param = {};
    param[key] = value;
    this.setData({
      formInfo: Object.assign({}, formInfo, param),
    });
  },

  // 方法二：表单提交时更新数据
  handleToSubmit(event) {
    /**
     * const { value } = event.detail;
     * const { formInfo } = this.data;
     * this.setData({
     *   formInfo: Object.assign({}, formInfo, value)
     * });
     */

    const { formInfo = {} } = this.data;
    if (!formInfo.name) {
      wx.showModal({
        content: "请输入姓名~",
        showCancel: false,
      });
      return false;
    }
    if (!formInfo.phone) {
      wx.showModal({
        content: "请输入手机号码~",
        showCancel: false,
      });
      return false;
    }
    if (!/\d{11}/.test(formInfo.phone)) {
      wx.showModal({
        content: "请输入正确格式的手机号码~",
        showCancel: false,
      });
      return false;
    }
    if (!formInfo.address) {
      wx.showModal({
        content: "请输入详细地址~",
        showCancel: false,
      });
      return false;
    }
    console.log(formInfo, "submit");
  },

  initValidate() {
    const rules = {
      name: {
        required: true,
      },
      tel: {
        required: true,
        tel: true,
      },
    };
    const messages = {
      name: {
        required: "请输入职位",
      },
      tel: {
        required: "请输入手机号",
        tel: "请输入正确格式手机号",
      },
    };
    this.WxValidate = new WxValidate(rules, messages);
  },

  formChange(val) {
    console.log(val, 'p[');
    let obj = {}
    obj[`searchForm.${val.currentTarget.dataset.key}`]= val.detail.value
    this.setData(obj)
},

  submitForm(e) {
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    console.log(this.searchForm, e);
    const params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      console.log(this.WxValidate.errorList, 'error');
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000
      })
      // this.showModal(error);
      return false;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //验证规则函数
    // 此处需要注意的是一定要在js文件中onLoad验证规则，否则编译会报checkform is not a function 
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
